﻿''' <summary>
''' 用于傳入一個DataTable ，返回選中項
''' </summary>
''' <remarks></remarks>
Public Class Frm_CasioSelectItems
    Public Sub New(ByVal infoText As String, Optional ByVal FormTitle As String = "Frm_CasioSelectItems")
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        Me.Label1.Text = infoText
        Me.Text = FormTitle
    End Sub

    Private TableIn As DataTable
    ''' <summary>
    ''' 傳入的供選擇的DataTable(其中第一列必須為bool值)
    ''' </summary>
    ''' <value>傳入的供選擇的DataTable(其中第一列必須為bool值)</value>
    ''' <remarks></remarks>
    Public WriteOnly Property InTable() As DataTable
        Set(ByVal value As DataTable)
            If ((Not value Is Nothing) And value.Rows.Count > 0 And value.Columns(0).DataType.Equals(GetType(Boolean))) Then
                TableIn = value
                TableOutSelected.Columns.Add("CasioID", GetType(String))
                TableOutSelected.Columns.Add("Item", GetType(String))
                TableOutSelected.AcceptChanges()
                TableOutUnSelected.Columns.Add("CasioID", GetType(String))
                TableOutUnSelected.Columns.Add("Item", GetType(String))
                TableOutUnSelected.AcceptChanges()
            End If
        End Set
    End Property

    ''' <summary>
    ''' 傳出的選中項(DataTable)
    ''' </summary>
    ''' <remarks></remarks>
    Private TableOutSelected As New DataTable
    ''' <summary>
    ''' 傳出的選中項(DataTable: CasioID,Item )
    ''' </summary>
    ''' <value></value>
    ''' <returns>傳出的選中項(DataTable: CasioID,Item )</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property OutTableSelected() As DataTable
        Get
            Return TableOutSelected
        End Get
    End Property

    Private TableOutUnSelected As New DataTable
    ''' <summary>
    ''' 傳出的沒有選中的項(DataTable: CasioID,Item )
    ''' </summary>
    ''' <value></value>
    ''' <returns>傳出的沒有選中的項(DataTable: CasioID,Item )</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property OutTableUnSelected() As DataTable
        Get
            Return TableOutUnSelected
        End Get
    End Property



    Private Sub Frm_CasioSelectItems_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.DataSource = Me.TableIn
        For Each col As DataGridViewColumn In Me.DataGridView1.Columns
            If col.Index = 0 Then
                col.ReadOnly = False
                col.HeaderText = "選擇"
            Else
                col.ReadOnly = True
            End If
        Next
        If Me.DataGridView1.Columns.Contains("Checked") Then
            Me.DataGridView1.Columns("Checked").HeaderText = "選擇"
        End If
        If Me.DataGridView1.Columns.Contains("CasioID") Then
            Me.DataGridView1.Columns("CasioID").HeaderText = "Casio編號"
        End If
        If Me.DataGridView1.Columns.Contains("LogType") Then
            Me.DataGridView1.Columns("LogType").HeaderText = "日志類型"
        End If
        If Me.DataGridView1.Columns.Contains("LogContent") Then
            Me.DataGridView1.Columns("LogContent").HeaderText = "日志內容"
        End If
        If Me.DataGridView1.Columns.Contains("OperateType") Then
            Me.DataGridView1.Columns("OperateType").HeaderText = "操作類型"
        End If
        If Me.DataGridView1.Columns.Contains("OperateTime") Then
            Me.DataGridView1.Columns("OperateTime").HeaderText = "操作時間"
        End If
    End Sub

    Private Sub prepareTB2()
        ''所有的都是沒有選擇的選項
        For Each row As DataRow In Me.TableIn.Rows
            '不重復添加到“未選擇清單”中
            If Me.TableOutUnSelected.Select("CasioID='" + row("CasioID").ToString() + "' and Item='" + row("LogContent").ToString() + "'").Length = 0 Then
                Dim outrowUns As DataRow = TableOutUnSelected.NewRow()
                outrowUns(0) = row("CasioID")
                outrowUns(1) = row("LogContent")
                TableOutUnSelected.Rows.Add(outrowUns)
                TableOutUnSelected.AcceptChanges()
            End If
        Next
        ''清空所有選中的選項清單
        Me.TableOutSelected.Rows.Clear()
    End Sub

    Private Sub prepareTB()
        '‘先找出所有已經選擇了的
        For Each row As DataRow In Me.TableIn.Rows
            If row(0).Equals(True) Then
                ''一旦選擇了，就不需要在重復添加到“選擇清單”中
                If Me.TableOutSelected.Select("CasioID='" + row("CasioID").ToString() + "' and Item='" + row("LogContent").ToString() + "'").Length = 0 Then
                    Dim outrow As DataRow = TableOutSelected.NewRow()
                    outrow(0) = row("CasioID")
                    outrow(1) = row("LogContent")
                    TableOutSelected.Rows.Add(outrow)
                    TableOutSelected.AcceptChanges()
                End If
            End If
        Next

        For Each row As DataRow In Me.TableIn.Rows
            '一旦選擇了，就不需要在添加到“未選擇清單”中
            If Me.TableOutSelected.Select("CasioID='" + row("CasioID").ToString() + "' and Item='" + row("LogContent").ToString() + "'").Length = 0 Then
                '不重復添加到“未選擇清單”中
                If Me.TableOutUnSelected.Select("CasioID='" + row("CasioID").ToString() + "' and Item='" + row("LogContent").ToString() + "'").Length = 0 Then
                    Dim outrowUns As DataRow = TableOutUnSelected.NewRow()
                    outrowUns(0) = row("CasioID")
                    outrowUns(1) = row("LogContent")
                    TableOutUnSelected.Rows.Add(outrowUns)
                    TableOutUnSelected.AcceptChanges()
                End If
            End If
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        prepareTB()
        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        prepareTB2()
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub Frm_CasioSelectItems_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If e.CloseReason = CloseReason.UserClosing Then
            prepareTB2()
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        End If
    End Sub

    Private Sub rdbUnSelectAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If row.IsNewRow = False Then
                row.Cells(0).Value = CheckBox1.Checked
            End If
        Next
    End Sub
End Class